﻿using Bright.NetCore.Api.Attributes;
using Microsoft.AspNetCore.Mvc.Controllers;

namespace Bright.NetCore.Api.Helpers
{
    /// <summary>
    /// 特性帮助
    /// </summary>
    public static class AttributesHelper
    {
        /// <summary>
        /// 判断当前接口是否实现了FilterDBLogAttribute特性
        /// </summary>
        /// <param name="controllerAction">控制器动作</param>
        /// <returns></returns>
        public static bool ImplementAttributes(ControllerActionDescriptor controllerAction)
        {
            return ImplementAttributes<RequestDBLogAttribute>(controllerAction);
        }

        /// <summary>
        /// 判断当前接口是否实现了指定特性
        /// </summary>
        /// <typeparam name="T">特性类型</typeparam>
        /// <param name="controllerAction">控制器动作</param>
        /// <returns></returns>
        public static bool ImplementAttributes<T>(ControllerActionDescriptor controllerAction) where T : Attribute
        {
            bool isExist = false;
            if (controllerAction != null)
            {
                var myAttribute = controllerAction.MethodInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
                if (myAttribute != null)
                {
                    isExist = true;
                }
            }
            return isExist;
        }
    }
}
